home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Macintosh / Janus0.1.sit / Janus0.1 / READ ME Janus 0.1 < prev    next >
Text File  |  1997-10-18  |  14KB  |  337 lines

  1. Janus 0.1 Release Notes / Documentation / Examples
  2. Peter Creath
  3. pjcreath@phoenix.princeton.edu
  4. -------------------------------------------------------------------------
  5. - What the hell is Janus?
  6.  
  7. Think of this as the PPC native equivalent of the ResEdit CODE editor.
  8.  
  9. In layman's terms, Janus is a fat binary (of course) which lets you find the
  10. PowerPC native code corresponding to 680x0 code.  Now you can patch your
  11. favorite native games to give yourself infinite ammo and lives too!
  12.  
  13. NOTE: This is not for the faint of heart.  If you can figure out the 680x0
  14. patches on your own, this is for you.  If not, find somebody who can.
  15.  
  16. In technical terms, Janus is (in its current incarnation) a reference tracer for
  17. PowerPC-native code.  This means it can search for references to a Toolbox
  18. trap (aka "symbol" in the new PPC way of doing things) and references to a
  19. subroutine, and can tell you what trap is being dispatched by what subroutine.
  20.  
  21. -------------------------------------------------------------------------
  22. - Symbols?  Trap dispatch subroutines?  HUH?
  23.  
  24. If you REALLY understand how PPC native code works, you can probably skip this
  25. section.
  26.  
  27. In the old 680x0 way of doing things, the opcodes from 0xA000 to 0xAFFF were
  28. reserved for "A-Traps".  Any A-Trap then called the subroutine referred to by the
  29. trap table.  Apple used this trap mechanism to implement their Toolbox.  If you
  30. have never seen _WaitNextEvent or _Debugger or some such, you need to brush up
  31. on your 680x0 assembly.
  32.  
  33. In the PowerPC world, we still have traps, but Apple decided to do something a
  34. little more clever.  Patching traps is a pain in the ass, and is slow.  Traps are
  35. essential shared code, so why not have a universal dispatcher for generic symbols
  36. and dispatch traps that way?  This is exactly what Apple did.
  37.  
  38. When a PowerPC native program wishes to call a trap, it sets up the function
  39. parameters in typical PPC calling fashion, then it sets two registers to the
  40. first 8 bytes of the trap name.  (ie: "WaitNext")  This small fragment of code
  41. (which is generated for each trap used) is called the symbol (or trap) glue.
  42.  
  43. In addition, when a PPC native application launches, it exports a symbol table
  44. (part of the as-of-yet-undocumented PEF format) which the Code Fragment
  45. Manager checks to make sure it can resolve all the symbols.  (ie: It makes
  46. sure it knows how to handle Debugger or ParamText, or whatever else
  47. may be exported).
  48.  
  49. This is where Janus comes in.
  50.  
  51. -------------------------------------------------------------------------
  52. - What does Janus do?
  53.  
  54. Janus reads in the PEF symbol table and lets you do all sorts of nifty stuff with
  55. it:
  56.     o    You can list all the symbols and their associated numbers.  This, if nothing
  57.         else, tells you what traps are being called in the program.  (You also
  58.         need the trap numbers for the other cool stuff you can do.)
  59.     
  60.     o    You can search for references to the symbols (or, actually, their
  61.         glue code).  In other words, if you enter in the symbol number for
  62.         GetNextEvent, it will show you every place GetNextEvent() is called
  63.         in the native code.
  64.     
  65.     o    You can find out what symbol is called by a specific glue routine.
  66.     
  67.     o    You can search for references to any subroutine at all.
  68.  
  69. -------------------------------------------------------------------------
  70. - Why do I care?
  71.  
  72. Well, there is no native PPC debugger (a la MacsBug).  OK, so I lied, there is a
  73. 2-machine debugger, but not all of us have the luxury of having two machines
  74. adjacent and networked.  This makes patching native code a real hassle (or
  75. impossibility usually).
  76.  
  77. There's also no native equivalent to the ResEdit CODE editor, with its
  78. searching for references and such.
  79.  
  80. What you can do now is, once you have made a patch to 680x0 code, you can
  81. search around your patch for "landmarks", the most convenient of which
  82. are traps (or symbols).  Then you search for those same landmarks in the
  83. native code using Janus, thereby narrowing down your search to a few
  84. dozen native instructions (if you're lucky).  Naturally, things aren't always
  85. this easy, but this does cover the bulk of native software out there.
  86.  
  87. -------------------------------------------------------------------------
  88. - General instructions
  89.  
  90. I use PowerPCdisas for my native code wanderings.  Hence, Janus prints
  91. the code offsets in terms of their position in the data fork (not their position
  92. in the PEF container).
  93.  
  94. -------------------------------------------------------------------------
  95. - Patching and native code tips, etc.
  96.  
  97. 4800xxxx is a BRA statement
  98. 60000000 is a NOP statement
  99.  
  100. "mflr" occurrs at the beginning of every subroutine
  101. "blr" occurs at the end of every subroutine
  102.  
  103. There are often many return points within a subroutine.  In 68k code, there
  104. was usually just one RTS.  In native code, rather than BRA to the end (over
  105. a bunch of code), it just RTS's immediately.
  106.  
  107. Code within segments is (so far) always in the same order in 68k & PPC
  108. native, however the native code IS NOT ALWAYS in the same order as
  109. the segments themselves.  In other words, your PPC code could have
  110. the equivalent object code to segment 10, then 3, then 4, then 7, then
  111. 1, and so on.  Thus, if you're having trouble finding a piece of equivalent
  112. code, look for landmarks near the beginning and end of your code segment
  113. and find out where your code segment is in the native code.  (This is
  114. a generally useful practice, as correlating hundreds of occurrences of
  115. traps can be hard on the eyes!)
  116.  
  117. You definitely need the Motorola PowerPC™ 601 User's Manual.  It's about
  118. $5 or so (or whatever shipping costs).
  119. (IBM     part # 52G7484,         (800) 769-3772)
  120. (Motorola    part # MPC601UM/AD,    (800) 521-6274)
  121.  
  122. -------------------------------------------------------------------------
  123. - Limitations, future plans, etc.
  124.  
  125. o    Janus only reads PEF (not XCOFF) files.
  126. o    Janus only looks in applications with the native code in the data fork.
  127. o    Janus only looks in the first code container in the PEF.
  128. o    Janus needs enough RAM to read in the entire code fragment.
  129.  
  130. o    If I run across some XCOFF files (enough to reverse engineer Apple's
  131.     implementation), I will add that functionality.
  132. o    If you find non-applications with PEF data, let me know.
  133. o    If you find any applications with more than one code container, let
  134.     me know.
  135. o    I will leave the RAM requirement as-is, for speed reasons.
  136.  
  137. Originally, this was going to entirely automate the matching process between
  138. 68k code and PowerPC native code.  However, the need for SOME utility forced
  139. me to throw together the components I had finished and toss it out the door.
  140.  
  141. That and the lack of interface make this version 0.1.  1.0 should automate
  142. the task and save a data file (which will also allow you to match code between
  143. versions, so you don't have to re-find your patch for each release).  It'll also
  144. have an interface.
  145.  
  146. Hopefully, I'll also have a built-in disassembler (and possibly assembler) in
  147. 1.0.  This isn't entirely equivalent to ResEdit's CODE editor right now -- it
  148. just does the same thing as far as searching for references and such.
  149.  
  150. -------------------------------------------------------------------------
  151. - Some examples of Janus in action, to give you a feel of how to use it.
  152.  
  153. Case Study:  Game-cheating patches to Pathways Into Darkness 2.0 (fat)
  154.  
  155. Janus won't help you find the 68k code to patch, so I've provided the 68k patches
  156. already.  I walk you through 2 patches, showing you how to find the native code
  157. corresponding to the patched areas and tell you how to patch them (w/ HexEdit).
  158. --------------------------
  159.  
  160. First patch:  We'd like to be invulnerable, so we need to axe the subtraction
  161. command at CODE 6+22B4 (which is done in the 680x0 code by NOP'ing it out).  So
  162. let's try to find the corresponding code in the PPC native code.
  163.  
  164. Unfortunately, CODE 6 doesn't have an trap calls in it, so we can't use the direct
  165. method (which would be looking for distinctive traps surrounding the code in
  166. question).  We're interested in 6+22B4, which is closely followed by a JSR to
  167. CODE 3+1CBA.  This looks a bit more promising, being preceded by some good
  168. traps.
  169.  
  170. DrawString:
  171. 0x0000D46C
  172. 0x0000D52C
  173. 0x0000DCA4
  174. 0x0000DD6C
  175. 0x0000DE54
  176. 0x0000E878
  177. 0x0000E9E0
  178. 0x0000EB84
  179. 0x0000ED94
  180. 0x0000F4B8
  181. 0x0000F6A4
  182. 0x0000F7F0
  183. 0x0000FC24
  184. 0x0000FE78
  185. 0x000102A4
  186. 0x000103C0
  187. 0x00010A3C
  188. 0x00010A6C
  189. 0x00011098
  190. 0x000114F8
  191. 0x00015234 x
  192. 0x0001FB18 x
  193. 0x00022B08 x
  194.  
  195. Well, that's a pretty large number, but we can eliminate the last three, since
  196. they're all fairly far from any other occurrence of DrawString.  Still too
  197. many though...
  198.  
  199. So let's look back a ways and see if there are any interesting traps.  Too many
  200. StringWidths near DrawStrings to be of use...  But the GetMainDevice and
  201. NewCWindow at 3+1966 and 3+19AC look good.
  202.  
  203. GetMainDevice:
  204. 0x0000D1F8
  205. 0x0000D2A8
  206. 0x0000F910
  207. 0x0000F9B8
  208. 0x00010D70
  209. 0x00010DF4
  210. 0x00011688
  211. 0x000116B0
  212. 0x000129FC
  213. 0x00012BD8
  214. 0x00013BF0
  215. 0x00013FD0
  216. 0x000179A4
  217. 0x0002FCA0
  218. 0x0002FCC8
  219. 0x00031874
  220. 0x00031BBC
  221. 0x00031BE0
  222. 0x00032B4C
  223. 0x00032BD8
  224.  
  225. NewCWindow:
  226. 0x0000D324
  227. 0x0000FA64
  228. 0x00010E68
  229. 0x00012AB4
  230. 0x00017A18
  231. 0x00022D64
  232.  
  233. Hmm.  I would've expected more NewCWindow's than GetMainDevices...
  234. Well, there's still a fair number left, so lets see how many of these have
  235. OffsetRects sandwiching the GetMainDevice.  (Note the JSR code 2+20D0
  236. at 3+19BE = 0x0001DA3C...we'll use that for our next patch)
  237.  
  238. OffsetRect:        GetMainDevice:        OffsetRect:        GetMainDevice:        OffsetRect:        NewCWindow:
  239. (none)            0x0000D1F8        0x0000D2A0        0x0000D2A8        0x0000D2E0        0x0000D324 x
  240. 0x0000F978        0x0000F910        0x0000F9B0        0x0000F9B8        0x0000F9F0        0x0000FA64
  241. (none)            0x00010D70        0x00010DCC        0x00010DF4        0x00010E2C        0x00010E68 x
  242.                 (none)            0x000129F4        0x000129FC        0x00012A38        0x00012AB4 x
  243.                                 (none)            0x00179A4        0x00017C8C        0x00017A18 x
  244.                                                 (none)                            0x00022D64 x
  245.  
  246. So now we know where 3+19AC is: 0x0000FA64.  So let's look for
  247. subroutines after that.  Well, we see the CloseWindow, but no SetWTitle...
  248. Well, let's just keep looking past this mammoth subroutine 'til we find another
  249. subroutine and try to regain our bearings.  Ah, here's one at 0x0000FFC4.
  250. It does a GetPort, SetPort, SetRect, then a JSR.  Well, that looks an awful lot
  251. like 3+1CBA, especially with the parameters passed to the JSR (0xE8 and
  252. 0x14).  How convenient.  (And yes, there is still some guesswork involved.)
  253.  
  254. Now let's ask Janus & ResEdit what references there are to this subroutine:
  255. 0x0001040C    3+1A48
  256. 0x0002776C    6+076E
  257. 0x00029D54    6+20B8
  258. 0x00029EA8    6+21E2
  259. 0x0002A034    6+22D4
  260.  
  261. Luckily enough, it's pretty obvious which subroutines here correlate to the
  262. references listed by ResEdit's CODE editor.  We want 6+22D4, so let's look
  263. around 0x0002A034.  Look up a little bit, and look, there's our subtraction
  264. command at 0x0002A004.  Well, the easiest way to bypass this is to avoid the
  265. storage of the new value back in 60(R3), so NOP out that command.
  266.  
  267. Change: 0x002A008 from 0xB0C30060 to 60000000
  268. Effect: Invulnerability.  You still say "ouch", but you don't lose any health.
  269.  
  270. --------------------------
  271. Second patch: Don't you hate having to be on the little red runes in order to
  272. save?  Let's fix that.  The 680x0 code is at CODE 2+0728.  We need to axe
  273. the branch statement with a NOP.
  274.  
  275. Well, it looks like we won't even need that CODE 2 we noticed above, since
  276. there's an OpenDeskAcc just above at 2+06EA.  That's pretty distinctive.
  277. And sure enough, Janus only finds one occurrence of it at 0x000195A8.
  278.  
  279. So we slowly work our way down both sets of code (a large monitor really
  280. helps here!).  Notice that the method of implementing a jump table in PPC code
  281. is to use a blr instruction (being preceded by something other than mtlr R0).
  282.  
  283. Well, the inline jump table could make things sticky, so let's look for the first
  284. occurrence of TickCount just after the OpenDeskAcc: 0x000195F8.  Look
  285. above a little bit, past the branch and JSR and you see the branch we'd like
  286. to get rid of at 0x000195E4.
  287.  
  288. Change: 0x000195E4 from 0x41820028 to 60000000
  289. Effect: You can save anywhere.
  290.  
  291. --------------------------
  292. Some more patches you can practice on:
  293.  
  294. Infinite crystal life:        7+3EE0    D16B 0004 -> 4E71 4E71
  295. Ammo-less monsters:    7+0610    4EBA 0C1C -> 4E71 4E71
  296.                     7+11F4    4EBA 0038 -> 4E71 4E71
  297. Infinite ammo:            7+415A    9F6A 0004 -> 4E71 4E71
  298.  
  299. And since CODE 7 doesn't contain any traps, you'll have to use the same kind of
  300. strategy I used for the first patch above...
  301.  
  302. -------------------------------------------------------------------------
  303. - Epilogue
  304.  
  305. If you have any questions, comments, suggestions, etc., feel free to send me
  306. e-mail at pjcreath@phoenix.princeton.edu
  307.                                                     - Peter
  308.  
  309. -------------------------------------------------------------------------
  310. (Paranoid) DISCLAIMER:
  311.  
  312. Layman's summary:
  313.  
  314. The program should work, but if it crashes, you get what you pay for.
  315. I can't control what people do with this program, nor can I be held liable
  316. for their actions.
  317.  
  318. Legalese:
  319.  
  320. THE AUTHOR (Peter J. Creath) MAKES NO WARRANTY OR REPRESENTATION,
  321. EITHER EXPRESS OR IMPLIED, WITH RESPECT TO THE SOFTWARE, ITS QUALITY,
  322. PERFORMANCE, MARCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
  323. AS A RESULT, THIS SOFTWARE AND ACCOMPANYING DOCUMENTATION IS
  324. PROVIDED "AS IS," AND YOU THE USER ARE ASSUMING THE ENTIRE RISK AS
  325. TO ITS QUALITY AND PERFORMANCE.
  326.  
  327. IN NO EVENT WILL THE AUTHOR (Peter J. Creath) BE LIABLE FOR ANY
  328. CONSEQUENTIAL, INCIDENTAL OR INDIRECT DAMAGES (INCLUDING DAMAGES
  329. FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
  330. INFORMATION, AND THE LIKE) ARISING OUT OF THE USE OF OR INABILITY TO USE
  331. THE SOFTWARE OR ACCOMPANYING DOCUMENTATION EVEN IF THE AUTHOR
  332. (Peter J. Creath) HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  333.  
  334. The author's liability for actual damages for any cause whatsoever,
  335. and regardless of the form of the action, will be limited to the purchase price
  336. of the Software ($0.00).
  337.